<template>
  <div class="room-list">
    <div class="header">
      <button class="back-btn" @click="goBack">← 返回</button>
      <h2>房间列表</h2>
      <button class="refresh-btn" @click="loadRooms">刷新</button>
    </div>

    <div class="rooms-container">
      <div v-if="loading" class="loading">加载中...</div>
      <div v-else-if="rooms.length === 0" class="empty">
        <p>暂无可用房间</p>
        <button class="btn btn-primary" @click="createRoom">创建房间</button>
      </div>
      <div v-else class="rooms">
        <div v-for="room in rooms" :key="room.id" class="room-card" @click="joinRoom(room.id)">
          <div class="room-header">
            <h3>{{ room.name }}</h3>
            <span class="room-status" :class="room.status">{{ getStatusText(room.status) }}</span>
          </div>
          <div class="room-info">
            <div class="info-item">
              <span class="label">玩家:</span>
              <span>{{ room.currentPlayers }}/{{ room.maxPlayers }}</span>
            </div>
            <div class="info-item">
              <span class="label">剧本:</span>
              <span>{{ room.script || '未选择' }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import axios from "axios";

export default {
  name: "RoomList",
  setup() {
    const router = useRouter();
    const rooms = ref([]);
    const loading = ref(true);

    const loadRooms = async () => {
      try {
        loading.value = true;
        const response = await axios.get("/api/rooms");
        if (response.data.success) {
          rooms.value = response.data.rooms;
        }
      } catch (error) {
        console.error("加载房间列表失败:", error);
      } finally {
        loading.value = false;
      }
    };

    const joinRoom = (roomId) => {
      router.push(`/room/${roomId}`);
    };

    const createRoom = () => {
      // 跳转到剧本选择页面
      router.push("/script-select");
    };

    const getStatusText = (status) => {
      const statusMap = {
        waiting: "等待中",
        playing: "游戏中",
        finished: "已结束",
      };
      return statusMap[status] || status;
    };

    const goBack = () => {
      router.push("/");
    };

    onMounted(() => {
      loadRooms();
    });

    return {
      rooms,
      loading,
      loadRooms,
      joinRoom,
      createRoom,
      getStatusText,
      goBack,
    };
  },
};
</script>

<style scoped>
.room-list {
  min-height: 100vh;
  background: #f5f5f5;
}

.header {
  background: white;
  padding: 15px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header h2 {
  font-size: 20px;
  font-weight: 600;
}

.back-btn,
.refresh-btn {
  background: none;
  border: none;
  color: #667eea;
  font-size: 16px;
  cursor: pointer;
  padding: 5px 10px;
}

.rooms-container {
  padding: 20px;
}

.loading,
.empty {
  text-align: center;
  padding: 60px 20px;
  color: #999;
}

.empty p {
  margin-bottom: 20px;
}

.rooms {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.room-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s;
}

.room-card:active {
  transform: scale(0.98);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.room-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.room-header h3 {
  font-size: 18px;
  font-weight: 600;
}

.room-status {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.room-status.waiting {
  background: #e3f2fd;
  color: #1976d2;
}

.room-status.playing {
  background: #fff3e0;
  color: #f57c00;
}

.room-status.finished {
  background: #f3e5f5;
  color: #7b1fa2;
}

.room-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.info-item {
  display: flex;
  font-size: 14px;
}

.label {
  color: #999;
  margin-right: 10px;
  min-width: 50px;
}

.btn {
  padding: 12px 24px;
  font-size: 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
}

.btn-primary {
  background: #667eea;
  color: white;
}

.btn-primary:active {
  background: #5568d3;
}
</style>

